package cn.mesmile.socket.controller;

import cn.mesmile.socket.server.WebSocketServer;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author zb
 * @date 2021/1/27 16:18
 * @Description webSocket 消息的【发送与接收】
 */
@RequestMapping("/api/v1/demo")
@RestController
public class DemoController {

    @GetMapping("/index")
    public ResponseEntity<String> index(){
        System.out.println("index = " +  "index");
        return ResponseEntity.ok("请求成功");
    }

    @GetMapping("/alpage")
    public ModelAndView page(){
        return new ModelAndView("websocket");
    }

    /**
     *  向指定的用户发送消息
     * @param message
     * @param toUserId
     * @return
     * @throws IOException
     */
    @GetMapping("/push/{toUserId}")
    public ResponseEntity<String> pushToWeb(@RequestParam("message") String message, @PathVariable String toUserId) throws IOException {
        WebSocketServer.sendInfo(message,toUserId);
        return ResponseEntity.ok("MSG SEND SUCCESS");
    }


    /**
     *  群发一段消息
     * @param message  消息体
     * @return
     * @throws IOException io异常
     */
    @GetMapping("/pushGroup")
    public ResponseEntity<String> pushToWeb(@RequestParam("message") String message) throws IOException {
        WebSocketServer.sendGroupMessage(message);
        return ResponseEntity.ok("MSG SEND SUCCESS");
    }


}
